﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Web.Task;
using PowerCreator.LMS.Task.Entity;

public partial class sysadmin_TaskManager_MessageManager : PowerCreator.LMS.Web.Base.BasePage
{
    MessageManag objMessageManag = new MessageManag();
    MessageUserInfoManag objMessageUserInfoManag = new MessageUserInfoManag();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
       
            string search = GetSearch();
            BinderPager(search);
            BinderGV(search, 1);
            InitPage();




        }
    }
    protected override void InitPage()
    {
        lbl_InfoTitle.Text = "消息通知管理";//GetLanageValueByKey("lab_ManagerPaperInfo_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lab_QeustionDiffculty.Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_QuestionDiffculty", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lab_Course.Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_CourseName", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lab_Ken.Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_Ken", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lab_QuestionContent.Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_QuestionContent", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lab_QuestonType.Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_QuestionType", PowerCreator.LMS.Language.LanagetItemType.Page);

        chkAll.Text = GetLanageValueByKey("chkbox_ManagerQuestion_chkboxAll", PowerCreator.LMS.Language.LanagetItemType.Page);
        chkCancel.Text = GetLanageValueByKey("chkbox_ManagerQuestion_chkboxCancel", PowerCreator.LMS.Language.LanagetItemType.Page);

        btn_DelCheck.Text = GetLanageValueByKey("btnDel", PowerCreator.LMS.Language.LanagetItemType.Page);
        //btn_ChangeQuestionState.Text = GetLanageValueByKey("btn_ManagerQuestion_ChangeQuestionState", PowerCreator.LMS.Language.LanagetItemType.Page);
        //btn_UnUserQuestion.Text = GetLanageValueByKey("btn_ManagerQuestion_UnUserQuestion", PowerCreator.LMS.Language.LanagetItemType.Page);
        //btn_QuestionIn.Text = GetLanageValueByKey("btn_ManagerQuestion_QuestionIn", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_AddMessage.Text = "添加新消息";//GetLanageValueByKey("btn_ManagerQuestionInfo_AddQuestion", PowerCreator.LMS.Language.LanagetItemType.Page);

        //ddlCourseList.Items[0].Text = GetLanageValueByKey("Msg_ManagerQuestion_PleaseCheck", PowerCreator.LMS.Language.LanagetItemType.Page);
        //ddlDiffculty.Items[0].Text = GetLanageValueByKey("Msg_ManagerQuestion_PleaseCheck", PowerCreator.LMS.Language.LanagetItemType.Page);
        //ddlQuestionTypeList.Items[0].Text = GetLanageValueByKey("Msg_ManagerQuestion_PleaseCheck", PowerCreator.LMS.Language.LanagetItemType.Page);


        //btnSearch.Text = GetLanageValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_SortSearch.Text = GetLanageValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);
        //btn_QuestionIn.Text = GetLanageValueByKey("btn_QuestionToIn_DaoruQuestion", PowerCreator.LMS.Language.LanagetItemType.Page);
        //btn_AddQuestion.Text = GetLanageValueByKey("btn_ManagerQuestionInfo_AddQuestion", PowerCreator.LMS.Language.LanagetItemType.Page);

        btn_DelCheck.Attributes.Add("onClick", "javascript:return confirm('" + GetLanageValueByKey("Msg_ManagerQuestion_DeletMsg", PowerCreator.LMS.Language.LanagetItemType.Page) + "');");

        base.InitPage();
    }
    private string GetSearch()
    {
        string search = string.Empty;
        string title=txt_SearchKey.Text.Trim();
        if (title != "")
        {
            search += " MsgTitle '%" + title + "%' " ;
        }
        return search.ToString();
    }
    private void BinderPager(string search)
    {
        AspNetPager1.RecordCount = objMessageManag.Count(search);
        AspNetPager1.PageSize = LMSPageSize;
     
    }

    protected void gvPaperList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string msgID = e.CommandArgument.ToString();
        if (e.CommandName == "mydelete")
        {
            #region 删除消息通知表相关记录
            DataTable dt = new DataTable();
            dt = objMessageUserInfoManag.Query("FKMsgUfMsgID =" + msgID);
            for (int j = 0; j < dt.Rows.Count; j++)
            {
                int msgUfID = int.Parse(dt.Rows[j]["MsgUfID"].ToString());
                objMessageUserInfoManag.Remove(msgUfID);
            }
            #endregion

            objMessageManag.Remove(int.Parse(msgID));

            string search = GetSearch();
            BinderPager(search);
            BinderGV(search, AspNetPager1.CurrentPageIndex);
        }
        if (e.CommandName == "myeditor")
        {
            string url = "EditMessage.aspx?id=" + e.CommandArgument.ToString();
            Script.RedirectURL(url);
        }
        if (e.CommandName == "see")
        {
            string url = "MessageUserInfoManager.aspx?id=" + e.CommandArgument.ToString();
            Script.RedirectURL(url);
        }


    }
    protected void gvPaperList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //if (e.Row.RowType == DataControlRowType.Header)
        //{
        //    e.Row.Cells[0].Text = GetLanageValueByKey("msg_PaperList_PaperID", PowerCreator.LMS.Language.LanagetItemType.Page);
        //    e.Row.Cells[1].Text = GetLanageValueByKey("lab_AddPaperInfo_PaperName", PowerCreator.LMS.Language.LanagetItemType.Page);

        //    e.Row.Cells[2].Text = GetLanageValueByKey("lab_BuilderPaper_PaperCountScore", PowerCreator.LMS.Language.LanagetItemType.Page);
        //    e.Row.Cells[3].Text = GetLanageValueByKey("msg_PaperList_PaperTime", PowerCreator.LMS.Language.LanagetItemType.Page);
        //    e.Row.Cells[4].Text = GetLanageValueByKey("msg_PaperList_EaxmPeopleCount", PowerCreator.LMS.Language.LanagetItemType.Page);
        //    e.Row.Cells[5].Text = GetLanageValueByKey("msg_PaperList_PassPeopleCount", PowerCreator.LMS.Language.LanagetItemType.Page);
        //    //e.Row.Cells[6].Text = GetLanageValueByKey("lab_AddPaperInfo_PaperSelectType", PowerCreator.LMS.Language.LanagetItemType.Page);
        //    //e.Row.Cells[9].Text = GetLanageValueByKey("lab_ManagerPaperInfo_PaperState", PowerCreator.LMS.Language.LanagetItemType.Page);
        //    //e.Row.Cells[10].Text = GetLanageValueByKey("btn_ManagerQuestion_Work", PowerCreator.LMS.Language.LanagetItemType.Page);
        //    //e.Row.Cells[8].Text = GetLanageValueByKey("btn_ManagerQuestion_QuestionState", PowerCreator.LMS.Language.LanagetItemType.Page);
        //    //e.Row.Cells[9].Text = GetLanageValueByKey("btn_ManagerQuestion_Work", PowerCreator.LMS.Language.LanagetItemType.Page);

        //}
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("bgColor", "#F7FAFF");
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#FAEDB8'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#F7FAFF'");

            //LinkButton btn_view = (LinkButton)e.Row.Cells[4].FindControl("btn_view");
            //btn_view.Text = GetLanageValueByKey("btn_PaperList_xiangqing", PowerCreator.LMS.Language.LanagetItemType.Page);

        }
    }

    private void BinderGV(string search, int pageIndex)
    {
      
        DataTable dt = new DataTable();
        dt = objMessageManag.Query(search, " MsgID desc", LMSPageSize, pageIndex);
        //gvMessage.Columns[4].HeaderText = GetLanageValueByKey("btn_ManagerQuestion_Work", PowerCreator.LMS.Language.LanagetItemType.Page);

        gvMessage.DataSource = dt;
        gvMessage.DataBind();

        PreAspNetPager(AspNetPager1);

    }
    protected void btn_DelCheck_Click(object sender, EventArgs e)
    {
        int checkcount = 0;
        for (int i = 0; i < gvMessage.Rows.Count; i++)
        {

            CheckBox objCheckBox = (CheckBox)gvMessage.Rows[i].FindControl("Chbox_Check");
            Label lab_MsgID = (Label)gvMessage.Rows[i].FindControl("lab_MsgID");
            if (objCheckBox.Checked)
            {
                checkcount += 1;
                int msgID = int.Parse(lab_MsgID.Text);
            
                #region 删除消息通知表相关记录
                DataTable dt = new DataTable();
                dt = objMessageUserInfoManag.Query("FKMsgUfMsgID =" + msgID);
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    int msgUfID = int.Parse(dt.Rows[j]["MsgUfID"].ToString());
                    objMessageUserInfoManag.Remove(msgUfID);
                }
                #endregion

                objMessageManag.Remove(msgID);

            }
        }
        if (checkcount == 0)
        {
            Script.Alert(this, "", GetLanageValueByKey("Msg_ManagerQuestion_PleaseCheck", PowerCreator.LMS.Language.LanagetItemType.Page));
            return;
        }
        Script.Alert(this, "", GetLanageValueByKey("Msg_ManagerQuestion_WorkOK", PowerCreator.LMS.Language.LanagetItemType.Page));
        string search = GetSearch();
        BinderPager(search);
        BinderGV(search, AspNetPager1.CurrentPageIndex);
    }
    protected void chkAll_CheckedChanged(object sender, EventArgs e)
    {
        chkCancel.Checked = false;
        //全选 / 取消
        for (int i = 0; i < gvMessage.Rows.Count; i++)
        {
            CheckBox cbox = (CheckBox)gvMessage.Rows[i].Cells[0].FindControl("Chbox_Check");
            if (chkAll.Checked)
                cbox.Checked = true;
            else
                cbox.Checked = false;
        }
    }
    protected void chkCancel_CheckedChanged(object sender, EventArgs e)
    {
        chkAll.Checked = false;
        //反选
        for (int i = 0; i < gvMessage.Rows.Count; i++)
        {
            CheckBox cbox = (CheckBox)gvMessage.Rows[i].Cells[0].FindControl("Chbox_Check");
            if (cbox.Checked)
                cbox.Checked = false;
            else
                cbox.Checked = true;
        }
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderGV(search, AspNetPager1.CurrentPageIndex);
    }
    protected void btn_AddMessage_Click(object sender, EventArgs e)
    {
        Script.RedirectURL("AddMessage.aspx");
    }
    protected void btn_SortSearch_Click(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderPager(search);
        BinderGV(search, 1);
    }
}
